package top.idcard.agent.biz.rw.read;

import top.idcard.agent.biz.rw.channel.RWContext;

import java.io.Closeable;
import java.io.IOException;

/**
 * @author wx
 * @version 1.0
 * @date 2021/2/3 4:35 下午
 */
public abstract class AbstractRead<I extends Closeable, T> implements Read<T>{

    protected I input;
    protected RWContext<I,T> channel;

    public AbstractRead(I input) {
        this.input = input;
        this.channel = RWContext.getInstance(this);
    }

    public T readAndWrite(){
        T data = channel.readData();
        channel.writeData();
        return data;
    }

//    protected void write(T data){
//        channel.writeData(data);
//    }

    public abstract T nextObject();

    public void closeRead(){
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public I getInput(){
        return input;
    }

    public abstract String readToType();
}
